import React from "react";
import VScrollEchartsV2 from "components/template/graphic-report/vscroll-echarts";
import ReportTable from "components/template/graphic-report/report-table";
import { getLocalStorageWithAutoParse , messages } from "share/common";
import { commonUtilsFactory, toggleShowLabel, popoverTitle } from "../echarts-data-transfer/utils";
import ComplianceDetailService from '../report-private-service/compliance-detail-report.service'
import GraphicReportService from "../graphic-report.service";
import { COMMON_REPORT_CONFIG, COMPLIANCE_DETAIL_REPORT } from "../constant";

const reportKeyPrefix = 'hly.reportLabel.32.';
const excessExpenseLabelKey = `${reportKeyPrefix}excessExpenseReport`; // 超标金额-费用类型分析
const excessUserLabelKey = `${reportKeyPrefix}excessUserReport`; // 超标金额-人员分析
const notExcessExpenseLabelKey = `${reportKeyPrefix}notExcessExpenseReport`; // 未超标金额-费用类型分析
const notExcessUserLabelKey = `${reportKeyPrefix}notExcessUserReport`; // 未超标金额-人员分析

/*
 * 合规明细报表
 * id 32
 */

// commonUtilsFactory目前其实只支持配置展示数值，如果要单独用导出还得包一层，见借款分析图表
// 这部分其实可以直接放到commonUtilsFactory中，但是借款分析图表表格的导出就是这样处理的，这里沿用一样的方式
// 后面如果图表中导出用的多的话，可以考虑把这里的逻辑改在commonUtilsFactory中
function exportUtilsFactory({ reportType, exportType, context, config, allowSetting }) {
  const reportCode = 'RSR';
  let ref = config.ref || {
    state: {}
  };
  let { list = [] } = ref.state || {}; // 表格是否有数据看reportTable的list
  let { dataLength = 0 } = config // 图形报表是否有数据看dataLength
  let exportDisable = !(list.length || dataLength);
  const { reportId } = context.state;
  let transferredParams = GraphicReportService.getCurrentTransferredParams({ context });
  let exportParams = { reportSearchDTO: { id: reportId, ...transferredParams } };
  return commonUtilsFactory({
    context,
    config,
    reportType,  // 用于修改label是否显示
    exportType,  // 导出
    reportKeyPrefix,  // 和reportType一起用于修改label是否显示
    allowSetting,
    allowExport: true,
    reportCode,
    exportDisable,
    exportParams
  });
}

export default (context) => {
  let excessExpenseShowLabel = getLocalStorageWithAutoParse(excessExpenseLabelKey);
  if (excessExpenseShowLabel === undefined) {
    excessExpenseShowLabel = true;
  }

  let excessUserShowLabel = getLocalStorageWithAutoParse(excessUserLabelKey);
  if (excessUserShowLabel === undefined) {
    excessUserShowLabel = true;
  }

  let notExcessExpenseShowLabel = getLocalStorageWithAutoParse(notExcessExpenseLabelKey);
  if (notExcessExpenseShowLabel === undefined) {
    notExcessExpenseShowLabel = true;
  }

  let notExcessUserShowLabel = getLocalStorageWithAutoParse(notExcessUserLabelKey);
  if (notExcessUserShowLabel === undefined) {
    notExcessUserShowLabel = true;
  }

  return {
    excessExpenseReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key183')/*超标金额-费用类型分析*/,
      fetchMethod: ComplianceDetailService.getExcessExpenseReport,
      showLabel: excessExpenseShowLabel,
      activeConfig: {
        key: 'filterExpTypeIds',
        array: false,
        notReset: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'EXCESS_EXP_NOT_OK';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='excessExpenseReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    excessUserReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key184')/*超标金额-人员分析*/,
      fetchMethod: ComplianceDetailService.getExcessUserReport,
      showLabel: excessUserShowLabel,
      activeConfig: {
        key: 'filterUserOid',
        array: false,
        notReset: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'EXCESS_USER_NOT_OK';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='excessUserReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    notExcessExpenseReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key185')/*未超标金额-费用类型分析*/,
      fetchMethod: ComplianceDetailService.getNotExcessExpenseReport,
      showLabel: notExcessExpenseShowLabel,
      activeConfig: {
        key: 'filterExpTypeIds',
        array: false,
        notReset: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'EXCESS_EXP_OK';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='notExcessExpenseReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    notExcessUserReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key186')/*未超标金额-人员分析*/,
      fetchMethod: ComplianceDetailService.getNotExcessUserReport,
      showLabel: notExcessUserShowLabel,
      activeConfig: {
        key: 'filterUserOid',
        array: false,
        notReset: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'EXCESS_USER_OK';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='notExcessUserReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    complianceDetailReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key187')/*合规明细统计表*/,
      fetchMethod: ComplianceDetailService.getUserFeeDetailReport,
      isTable: true,
      columns: popoverTitle(COMPLIANCE_DETAIL_REPORT),
      render: ({ config, context }) => {
        const exportType = 'EXP_EXCESS_DETAIL';
        const utils = exportUtilsFactory({ exportType, context, config, allowSetting: false });

        return (
          <ReportTable
            title={config.title}
            cols={24}
            utils={utils}
            columns={config.columns}
            fetchMethod={config.fetchMethod}
            onRef={context.onMountCharts}
            dataLength={1}
            reportKey='complianceDetailReport'
          />
        )
      }
    }
  }
}
